
var express = require('express');
var fs = require('fs');
var process = require('child_process');
var app = express();
var state=0,
    r;

/**
 * 作者使用校园网 需要验证
 */
    state=setInterval(()=>{
        process.exec('curl --compressed https://www.baidu.com/img/bd_logo1.png',function (error, stdout, stderr){

            if (error !== null) {

                var process = require('child_process');
                process.exec('curl "http://192.168.100.200/login?DDDDD=&upass=&0MKKey=login"',function (error, stdout, stderr){
                    
                    if (error !== null) {
                        console.log("can't login in!");
                        clearInterval(state);
                        return;
                    }

                    console.log('login');
                    console.log('');
                    
                });



                return;
            }

            console.log('keep');
        });
    
    },120000);



app.get('/', function (req, res) {
    r=canshu(req);
    console.log('require:'+r+'.xml');

    /*
    fs.exists('danmu/'+canshu(req)+'.xml',(reqq)=>{
        if(reqq){

            fs.readFile('danmu/'+canshu(req)+'.xml','utf8', (err, data) => {
                    if (err) throw err;
                    res.set('Content-Type', 'text/xml');
                    res.send(data);

                    console.log('.xml exist,send!');
                    console.log('');

              });

        }else{

            //get(canshu(req));
    */
            console.log('get');

            


            process.exec('curl --compressed https://api.bilibili.com/x/v1/dm/list.so?oid='+r,{
                maxBuffer: 5000 * 1024
            },function (error, stdout, stderr){
                   
                if (error !== null) {

                    console.log('net break!')
                    console.log('');
                    console.log(error);
                    console.log('');

                    fs.exists('danmu/'+r+'.xml',(reqq)=>{
                        if(reqq){
                
                            fs.readFile('danmu/'+r+'.xml','utf8', (err, data) => {
                                    if (err) throw err;
                                    res.header('Access-Control-Allow-Origin', '*');
                                    res.set('Content-Type', 'text/xml');
                                    res.send(data);
                
                                    console.log('.xml exist,send!');
                                    console.log('');
                
                            });
                        }else{
                            res.header('Access-Control-Allow-Origin', '*');
                            res.status(404).send('');
                            console.log('.xml no exist');
                            console.log('');
                        }
                    });
                    
                    return;
                }
                
                res.header('Access-Control-Allow-Origin', '*');
                res.set('Content-Type', 'text/xml');
                res.send(stdout);
                console.log('send!');

                write(stdout);
                console.log('save!');
                console.log('');

            });
        
            //res.status(404).send('<script>setTimeout(()=>{window.location.reload();},1000);</script>');

        //}
    //});
    
});

app.listen(3000, function () {
  console.log('danmu_get.js at port 3000');
});


function canshu(req){
    var urll=req.url;
    var uop=urll.indexOf('r=')+2;
    return urll.substring(uop);
}


/*function get(r){
    var process = require('child_process');
    process.exec('curl --compressed https://api.bilibili.com/x/v1/dm/list.so?oid='+r,function (error, stdout, stderr){
            if (error !== null) {
                
                console.log('exec error: ' + error);
                return;
            }
    });
}
*/
function write(buf){
    fs.writeFile('danmu/'+r+'.xml',buf,function(){
    });
}

